package demo2;

import demo2.loginexception.PasswordErrorException;
import demo2.loginexception.UserNameErrorException;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA
 * Description
 * User：chenshu
 * Date：2023-10-25
 * Time：09:07
 */
class Login {
    private String userName = "admin";
    private String password = "123456";

    public Login(String userName, String password) {
        this.userName = userName;
        this.password = password;
    }

    public void loginInfo(String userName, String password) throws PasswordErrorException,
            UserNameErrorException{
        if (!this.userName.equals(userName)) {
            throw new UserNameErrorException("用户名错误");
        }

        if (!this.password.equals(password)) {
            throw new PasswordErrorException("密码错误");
        }

        System.out.println("登陆成功");
    }
}

class Student implements Cloneable {
    int age = 10;
    String name = "cs";

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}


public class TestDemo {
    public static void main(String[] args) {
        Login login = new Login("admin", "123456");
        try{
            login.loginInfo("cs", "123456");
        } catch (UserNameErrorException e1) {
            e1.printStackTrace();
        } catch (PasswordErrorException e2) {
            e2.printStackTrace();
        }

        System.out.println("其他业务");

    }
    /**
     * finally的特性
     *
     * @return
     */
    public static int func3() {
        try {
            System.out.println(10 / 10);
            return 10;
        } catch (ArithmeticException e) {
            System.out.println("捕获到算数异常!");
        } finally {
            return -1;//尽量不要再finally语句中return
        }
    }

    public static void main5(String[] args) {
        System.out.println(func3());
    }

    public static void main4(String[] args) {
        Scanner scanner = new Scanner(System.in);
        try {
            int a = scanner.nextInt();
            System.out.println(10 / a);
            System.out.println("aaaaa");
        } catch (ArithmeticException e) {
            System.out.println("捕获到了算数异常！");
            e.printStackTrace();
        } finally {
            scanner.close();
            System.out.println("finally一般用于关闭资源，无论是否发生异常，finally中的代码都会被执行");
        }
        System.out.println("其他业务逻辑");
    }

    /**
     * 处理受查异常的两种方式
     *
     * @param a
     */
    //2、声明异常throws，但不处理
    public static void func2(int a) throws CloneNotSupportedException {
        if (a == 10) {
            throw new CloneNotSupportedException("a==10");
        }
    }

    //1、捕获并处理
    public static void func(int a) {
        try {
            if (a == 10) {
                throw new CloneNotSupportedException("a==10");
            }
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
    }

    /**
     * 对于情况2的两种处理方法
     *
     * @param args
     * @throws CloneNotSupportedException
     */
    //2、只能保证程序编译通过，还是没有处理异常，交给JVM
    public static void main3(String[] args) throws CloneNotSupportedException {
        func2(10);
        System.out.println("其他业务");
    }

    //1、处理声明的异常
    public static void main2(String[] args) {
        try {
            func2(10);
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }

        System.out.println("其他业务");
    }

    public static void main1(String[] args) {
        Student stu1 = new Student();
        //受查异常
        //Student stu2 = (Student) stu1.clone();
        //非受查异常
        int[] arr = {1, 2, 3, 4};
        System.out.println(arr[9]);
    }
}
